package cz.hanis.android.geography.activities;

import java.util.ArrayList;
import java.util.List;

import cz.hanis.android.geography.R;
import cz.hanis.android.geography.TestActivity;
import cz.hanis.android.geography.models.Country;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class CountryCapitalTestActivity extends TestActivity {

	
	private TextView tvCountry;	
	private int correct;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTestLayout(R.layout.country_capital_test);
		setQuestion(getString(R.string.tv_country_capital_question));
		tvCountry = (TextView) findViewById(R.id.tv_country_capital_country);
		setQuestionView(tvCountry);
		addAnswerView((Button) findViewById(R.id.bt_country_capital_capital1));
		addAnswerView((Button) findViewById(R.id.bt_country_capital_capital2));
		addAnswerView((Button) findViewById(R.id.bt_country_capital_capital3));
		addAnswerView((Button) findViewById(R.id.bt_country_capital_capital4));
		setListeners();
		
		if (savedInstanceState == null) {
			newRound();
		} else {
			tvCountry.setText(savedInstanceState.getString(QUESTION_KEY));
			correct = savedInstanceState.getInt(CORRECT_POSITION_KEY);
		    ArrayList<String> answerList = savedInstanceState.getStringArrayList(ANSWER_LIST_KEY);	    
			for(int i = 0; i < getNumberOfAnswers(); i++) {
				Button button = (Button) getAnswerViews().get(i);
				button.setText(answerList.get(i));
			}
			
			restoreRound();
		}
		
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	    outState.putInt(CORRECT_POSITION_KEY, correct);
	    outState.putString(QUESTION_KEY, tvCountry.getText().toString());	    	    
	    ArrayList<String> answerList = new ArrayList<String>();	    
		for(int i = 0; i < getNumberOfAnswers(); i++) {
			Button button = (Button) getAnswerViews().get(i);
			answerList.add(button.getText().toString());
		}
		outState.putStringArrayList(ANSWER_LIST_KEY, answerList);	    
	}	
	
	
	@Override
	protected int onAnswer(int answer) {		
		int result = TestActivity.ANSWERE_NONE;
		setBackgroudButton(correct, R.drawable.button_9patch_green);
		
		if (answer == correct) {
			result = TestActivity.ANSWERE_CORRECT;
		} else if (answer == TestActivity.ANSWERE_TIMED_OUT) {
			result = TestActivity.ANSWERE_TIMED_OUT;
		} else {
			setBackgroudButton(answer, R.drawable.button_9patch_red);
			result = TestActivity.ANSWERE_INCORRECT;
		}
		return result;
	}

	@Override
	protected void onNewRound() {				
		List<Country> randomCountries = getTools().getRandomCountries(getNumberOfAnswers(),
				getTest().getContinent());
		correct = getTools().getRandom(getNumberOfAnswers());
		String name = randomCountries.get(correct).getName();
		tvCountry.setText(name);
		
		for(int i = 0; i < getNumberOfAnswers(); i++) {
			setBackgroudButton(i, R.drawable.button_9patch);
			Button button = (Button) getAnswerViews().get(i);
			button.setText(randomCountries.get(i).getCapital());
		}
	}

	@Override
	protected void onRestoreRound() {
	}
		
	
	
	
}
